package com.cassiopae.services;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cassiopae.dao.ProfileDaoImpl;
import com.cassiopae.dao.UserDaoImpl;
import com.cassiopae.entity.Profile;
import com.cassiopae.entity.User;

/**
 * Servlet implementation class ActionServlet
 */
@WebServlet("/ActionServlet")
public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserDaoImpl userDaoImpl = new UserDaoImpl();
	ProfileDaoImpl profileDaoImpl = new ProfileDaoImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ActionServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");


		if ( "supprimerUser".equals(action)) { 
			String login = request.getParameter("deleteUserID");
			userDaoImpl.delete(login);
			User user = (User) request.getSession().getAttribute("user");
			List<User> users = userDaoImpl.getAllWithoutLogin(user.getEmail());
			request.setAttribute("LISTUSER", users);
			getServletContext().getRequestDispatcher("/listUser.jsp").forward(request, response);
		}

		if ("modifierUser".equals(action)){
			String login = request.getParameter("login");
			String nom = request.getParameter("nom");
			String prenom = request.getParameter("prenom");
			userDaoImpl.updateUser(nom, prenom, login);
			User user = (User) request.getSession().getAttribute("user");
			List<User> users = userDaoImpl.getAllWithoutLogin(user.getLogin());
			request.setAttribute("LISTUSER", users);
			getServletContext().getRequestDispatcher("/listUser.jsp").forward(request, response);
		}

		if ("supprimerSchema".equals(action)){
			int id = Integer.parseInt(request.getParameter("deleteProfileID"));
			profileDaoImpl.delete(id);
			List<Profile> profiles = profileDaoImpl.getAll();
			request.getSession().setAttribute("LISTPROFIL", profiles);
			getServletContext().getRequestDispatcher("/listProfile.jsp").forward(request, response);

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
